//
//  CHPopUpViewController.m
//  demo
//
//  Created by xinhualong on 2024/9/19.
//

#import "CHPopUpViewController.h"

@interface CHPopUpViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *yearsArray;
@property (nonatomic, strong) NSArray *monthsArray;
@property (nonatomic, strong) NSArray *daysArray;

@property (nonatomic, assign) NSInteger selectedYear;
@property (nonatomic, assign) NSInteger selectedMonth;
@property (nonatomic, assign) NSInteger selectedDay;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@end

@implementation CHPopUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
 
    
    NSMutableArray *years = [[NSMutableArray alloc] init];
    for (int i = 1900; i <= [self currentYear]; i++) {
        [years addObject:[NSString stringWithFormat:@"%d", i]];
    }
    self.yearsArray = years;
    
    self.monthsArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12"];
    
    self.selectedYear =  [[self.yearsArray firstObject] integerValue];
    self.selectedMonth = 1;
    

    [self updateDaysArrayWithYear:self.selectedYear month:self.selectedMonth];
    
    NSArray *array = [CHString(self.startDate) componentsSeparatedByString:@"/"];
    if(array.count==3){
        self.selectedYear = [array[2] integerValue];
        NSUInteger index = [self.yearsArray indexOfObject:[NSString stringWithFormat:@"%ld",self.selectedYear]];
        if (index!= NSNotFound) {
            [self.pickerView selectRow:index inComponent:2 animated:NO];
        }
        
        
        self.selectedMonth = [array[1] integerValue];
        NSUInteger indexMonth = [self.monthsArray indexOfObject:[NSString stringWithFormat:@"%ld",self.selectedMonth]];
        if (indexMonth!= NSNotFound) {
            [self.pickerView selectRow:indexMonth inComponent:1 animated:NO];
        }
        
        
        [self updateDaysArrayWithYear:self.selectedYear month:self.selectedMonth];
        
        self.selectedDay = [array[0] integerValue];
        NSUInteger indexDay = [self.daysArray indexOfObject:[NSString stringWithFormat:@"%ld",self.selectedDay]];
        if (indexDay!= NSNotFound) {
            [self.pickerView selectRow:indexDay inComponent:0 animated:NO];
        }
      
    }

}


- (NSInteger)currentYear {
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:date];
    return [components year];
}

#pragma mark - UIPickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 2) {
        return self.yearsArray.count;
    } else if (component == 1) {
        return self.monthsArray.count;
    } else {
        return self.daysArray.count;
    }
}

#pragma mark - UIPickerView Delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    pickerView.subviews[1].backgroundColor = UIColor.clearColor;
    if (component == 2) {
        return self.yearsArray[row];
    } else if (component == 1) {
        return self.monthsArray[row];
    } else {
        return self.daysArray[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 2) {
   
        self.selectedYear = [self.yearsArray[row] integerValue];
    } else if (component == 1) {
 
        self.selectedMonth = [self.monthsArray[row] integerValue];
    }else{
        self.selectedDay = [self.daysArray[row] integerValue];
    }
    

    if (component == 0 || component == 1) {
        [self updateDaysArrayWithYear:self.selectedYear month:self.selectedMonth];
        [self.pickerView reloadComponent:2];
    }
}

#pragma mark -

- (void)updateDaysArrayWithYear:(NSInteger)year month:(NSInteger)month {
    NSInteger daysInMonth = [self daysInMonth:month year:year];
    
    NSMutableArray *days = [[NSMutableArray alloc] init];
    for (int i = 1; i <= daysInMonth; i++) {
        [days addObject:[NSString stringWithFormat:@"%d", i]];
    }
    self.daysArray = days;
}

- (NSInteger)daysInMonth:(NSInteger)month year:(NSInteger)year {
    if (month == 2) {
        // 闰年判断
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return 29;
        } else {
            return 28;
        }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    } else {
        return 31;
    }
}
- (IBAction)clickOk:(id)sender {
    
    __weak typeof(self) weakSelf =self;
    [self dismissViewControllerAnimated:YES completion:^{
        if(weakSelf.okblock){
            weakSelf.okblock([NSString stringWithFormat:@"%02ld/%02ld/%ld",(long)weakSelf.selectedDay,(long)weakSelf.selectedMonth,(long)weakSelf.selectedYear]);
        }
    }];
}
- (IBAction)cliclClose:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
